
#!/usr/bin/env python2
# coding:utf-8

import random
import os
import eventlet
import task

@task.rtk()
def task_1(**kwargs):
    working_time = random.randrange(1, 10)
    print "Task_1 will running %s s"%working_time
    eventlet.sleep(working_time)
#     os.makedirs('./task111')
    print "Task_1 is commplet" 
    
def task_2():
    working_time = random.randrange(1, 10)
    print "Task_2 will running %s s"%working_time
    eventlet.sleep(working_time)
#     print "Task_2 is commplet" 
#     eventlet.sleep(random.randrange(3))
#     if random.randint(1, 10) > 3:
#         print "Task_2 failed" 
#         raise task.Failure(1)
    return "Taks_2 complet"    

def task_3():
    working_time = random.randrange(1, 10)
    print "Task_3 will running %s s"%working_time
    eventlet.sleep(working_time)
#     print "Task_3 is commplet" 
#     eventlet.sleep(random.randrange(3))
#     if random.randint(1, 10) > 3:
#         print "Task_3 failed"
#         raise task.Failure(1)
    return "Taks_3 complet"    

@task.rtk()    
def task_all(**kwargs):
    pool = eventlet.GreenPool()
    
    begin = True
    while begin:
        
        pool.spawn(task_1)
        pool.spawn(task_3)
        pool.spawn(task_2)
        print "run run"
        begin = False
        
        eventlet.Queue
#     a = pool.spawn(task_1)
    
#     import inspect
#     for i in  inspect.getmembers(a, inspect.ismethod):
#         print i
#     while True:
#         eventlet.spawn(task_1)
#     pool.spawn(task_2)
#      
#     pool.spawn(task_3)        
#     task_1()
#     task_2()
#     task_3()
#     task(taskall, ta_1, tas_2)
    
def recover(self, op="all"):
    lfile = self.config.home + "/../recover_lock"
    lock = _lock_file(lfile)

    if (op == "all"):
        for i in self.instences:
            if (i.running() == False):
                continue

            try:
                instence_scan = InstenceScan(i)
                instence_scan.recover()
            except Exp, e:
                _derror("%ss" % (e.err))
                exit(e.errno)    
    
if __name__ == '__main__':
    print "All task run"
#     task.setup_db('sqlite:///xxx.sqlite') 
#     task_id = task_all()
#     print task.is_complete(task_id)
#     task.run(task_id)
#     print task.get(task_id)["progress"]
    task_all()
    print "All right"
    import inspect    
    